package View.JTable;

import java.io.File;
import java.sql.Date;

import javax.swing.table.AbstractTableModel;

import Management.AdjusterMachine;
import Management.Animation;
import View.MainUI;

public class JAnimationTableModel extends AbstractTableModel {
	/**
	 * serialVersionUID;
	 */
	private static final long serialVersionUID = 7707912132188644269L;
	public final static int RULED 	= 0;
	public final static int NAME 	= 1;
	public final static int TYPE 	= 2;
	public final static int TIME 	= 3;
	public final static int SIZE 	= 4;
	
	public String[] columnName		= new String[5];
	public Object[][] values;
	
	/**
	 * 构造方法
	 * @param animatorData  数据，是一组的Animator;
	 * @param keyColumnName 一组关键字，决定一些变化的列;
	 * @param rawCount		行数;
	 * @param index			the index of RuleList to get the Rule;
	 */
	public JAnimationTableModel(File directory, int index) {
		columnName[0] = MainUI.s.ruled;
		columnName[1] = MainUI.s.name;
		columnName[2] = MainUI.s.type;
		columnName[3] = MainUI.s.time;
		columnName[4] = MainUI.s.size;
		setFileDirectory(directory , index);
	}

	/**
	 * set the file directory;
	 * @param directory
	 * @param index
	 */
	public void setFileDirectory(File directory , int index) {
		Animation[] animatorData = initialAnimator(directory);
		if (animatorData == null) {
			return;
		}
		int rawCount  = animatorData.length;

		values 		= new Object[rawCount][5];
		
		for (int i = 0; i < rawCount; i++) {
			AdjusterMachine am = new AdjusterMachine(index, animatorData[i].getAnimeFile());
			values[i][0] = am.isAdjusted();             	//boolean
			values[i][1] = animatorData[i].getAnimeFile(); 		//File
			if (!animatorData[i].getAnimeFile().isDirectory()) {
				values[i][2] = animatorData[i].getFileType();	//String
			}
			else {
				values[i][2] = "Directory";
			}
			Date date = new Date(animatorData[i].getLastModified());
			values[i][3] = date.toString();                    //String
			Long s = new Long(animatorData[i].getSize()%1024%1024);
			values[i][4] = s;                                  //long
		}
	}
	
	/**
	 * initialAnimator, make the directory become Animator[];
	 * @param directory
	 * @return
	 */
	private Animation[] initialAnimator(File directory) {
		if (directory.isDirectory()) {
			File[]	f = directory.listFiles();
			if (f == null) {
				return null;
			}
			Animation[] anime = new Animation[f.length];
			for (int i = 0; i < f.length; i++) {
				anime[i] = new Animation(f[i]);
			}
			return anime;
		}
		else {
			return null;
		}
	}
	
	@Override
	public boolean isCellEditable(int row, int column) {
		if (column == NAME) {
			return true;
		}
		return false;
	} 

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		System.out.println(aValue);
		System.out.println(rowIndex);
		System.out.println(columnIndex);
	}
	
	@Override
	public int getColumnCount() {
		int i = 5;
		return i;
	}

	@Override
	public int getRowCount() {
		return values.length;
	}

	@Override
	public Object getValueAt(int row, int column) {
		return values[row][column];
	}

	@Override
	public String getColumnName(int column) {
	    return columnName[column];
	} 
	
	@Override
    public Class getColumnClass(int column) {
        Class dataType = super.getColumnClass(column);
        if (column == RULED) {
          dataType = Boolean.class;
        }
        else if (column == NAME) {
        	dataType = File.class;
        }
        else if (column == TYPE) {
        	dataType = String.class;
        }
        else if (column == TIME) {
        	dataType = String.class;
        }
        else if (column == SIZE) {
        	dataType = Long.class;
        }
        return dataType;
      } 

}
